package com.xx.pmis.web.actions;

import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.xx.pmis.model.Role;
import com.xx.pmis.model.User;
import com.xx.pmis.services.RoleService;
import com.xx.pmis.services.UserService;
import com.xx.pmis.web.forms.UserForm;
import com.xx.pmis.web.util.CurrentDate;
import com.xx.pmis.web.util.Rand;

/**
 * @class 用户管理Action类 <br>
 * 
 * @editor 石星星  @Date 2011-9-21 下午03:17:42 <br>
 * @version jdk_1.5.0
 */
public class UserAction extends PMISBaseAction {
	private static final Log log = LogFactory.getLog(UserAction.class);
	RoleService roleService = getPMISServices().getRoleService();
	UserService userService = getPMISServices().getUserService();
	// 查询所有用户
	public ActionForward listUser(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		List<?> all = null;
		try {
			all = userService.getAllUser();
		} catch (Exception e) {
			log.info("【系统管理/用户管理】查询所有用户异常！"+ e.toString());
		}
		request.setAttribute("userList", all);
		request.setAttribute("position", "系统管理/用户");
		return mapping.findForward("listUser");
	}
	
	// 新增用户
	public ActionForward addUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		request.setAttribute("userRegDate", CurrentDate.currentDate());
		request.setAttribute("position", "用户/新增");
		return mapping.findForward("addUser");
	}
	
	// 新增，处理方法
	public ActionForward addUser_Do(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		UserForm f = (UserForm) form;
		String userName = f.getUserName();                //用户名
		String userPassword = f.getUserPassword();        //密码
		String userRegDate = f.getUserRegDate();          //注册日期
		String userDesc= f.getUserDesc(); // 描述
		
	    String loginName = f.getLoginName();

		// 用户
		User user = new User();
		user.setUserName(userName);
		// 用户编码自动产生，不能修改
		user.setUserCode("10" + Rand.getRand());
		user.setUserPassword(userPassword);
		user.setUserRegDate(userRegDate);
		user.setUserModDate(userRegDate);
		user.setUserState("1");
		user.setUserDesc(userDesc);
		user.setLoginName(loginName);
		// 用户角色，默认为一般用户
		Role role = null;
		try {
			role = roleService.getRoleByRoleCode("209999");
		} catch (Exception e) {
			log.info("【系统管理/用户管理/新增】根据角色名称查询该角色异常！"+ e.toString());
		}
		user.setRole(role);
		try {
			userService.saveOrUpdateUser(user);
		} catch (Exception e) {
			log.info("【系统管理/用户管理/新增】新增或更新用户异常！"+ e.toString());
			e.printStackTrace();
		}
	
		listUser(mapping, form, request, response);
		return mapping.findForward("listUser");
	}
	
	// 编辑
	public ActionForward editUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String userID = request.getParameter("userID");
		// 用户
		User user = null;
		try {
			user = userService.getUserByUserID(userID);
		} catch (Exception e) {
			log.info("【系统管理/用户管理/编辑】根据用户ID查询该用户异常！"+ e.toString());
		}
		// 最后一次修改日期
		request.setAttribute("userModDate", CurrentDate.currentDate());
		request.setAttribute("user", user);
		request.setAttribute("position", "用户/编辑");
		return mapping.findForward("editUser");
	}

	// 编辑，处理方法
	public ActionForward editUser_Do(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		UserForm f = (UserForm) form;
		String userID = f.getUserID();
		String userName = f.getUserName();                // 用户名
		String userPassword = f.getUserPassword();        // 密码
		String userModDate = f.getUserModDate();          // 修改日期
		String userDesc= f.getUserDesc(); // 描述
		String loginName = f.getLoginName();

		// 用户
		User user = null;
		try {
			user = userService.getUserByUserID(userID);
		} catch (Exception e) {
			log.info("【系统管理/用户管理/编辑】根据用户ID查询该用户异常！"+ e.toString());
		}
		user.setUserName(userName);
		user.setUserPassword(userPassword);
		user.setUserModDate(userModDate);
		user.setUserState("1");
		user.setUserDesc(userDesc);
		user.setLoginName(loginName);

		try {
			userService.saveOrUpdateUser(user);
		} catch (Exception e) {
			log.info("【系统管理/用户管理/编辑】新增或更新用户异常！"+ e.toString());
			e.printStackTrace();
		}
		listUser(mapping, form, request, response);
		return mapping.findForward("listUser");
	}

	// 删除
	public ActionForward delUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// 用户
		User user = null;
		UserForm f = (UserForm) form;
		String[] ids = f.getIds();
		for (int i = 0; i < ids.length; i++) {
			try {
				user = userService.getUserByUserID(ids[i]);
			} catch (Exception e) {
				log.info("【系统管理/用户管理/删除】根据用户ID查询该用户异常！"+ e.toString());
			}
			try {
				userService.deleteUser(user);
			} catch (Exception e) {
				log.info("【系统管理/用户管理/删除】新增或更新用户异常！"+ e.toString());
			}
		}

		listUser(mapping, form, request, response);
		return mapping.findForward("listUser");
	}
	
    // 启用用户
	public ActionForward startUser(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String userID = request.getParameter("userID");

		// 用户
		User user = null;
		try {
			user = userService.getUserByUserID(userID);
		} catch (Exception e) {
			log.info("【系统管理/用户管理/启用】根据用户ID查询该用户异常！"+ e.toString());
		}
		user.setUserState("0");

		try {
			userService.saveOrUpdateUser(user);
		} catch (Exception e) {
			log.info("【系统管理/用户管理/启用】新增或更新用户异常！"+ e.toString());
		}
		listUser(mapping, form, request, response);
		return mapping.findForward("listUser");
	}
	
    // 启用用户
	public ActionForward stopUser(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String userID = request.getParameter("userID");

		User user = null;
		try {
			user = userService.getUserByUserID(userID);
		} catch (Exception e) {
			log.info("【系统管理/用户管理/停用】根据用户ID查询该用户异常！"+ e.toString());
		}
		user.setUserState("1");

		try {
			userService.saveOrUpdateUser(user);
		} catch (Exception e) {
			log.info("【系统管理/用户管理/停用】新增或更新用户异常！"+ e.toString());
		}
		listUser(mapping, form, request, response);
		return mapping.findForward("listUser");
	}
	
	// 角色设置
	public ActionForward changeRole(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String userID = request.getParameter("userID");
		// 用户
		User user = null;
		try {
			user = userService.getUserByUserID(userID);
		} catch (Exception e) {
			log.info("【系统管理/用户管理/角色设置】根据用户ID查询该用户异常！"+ e.toString());
		}
		List<?> roleList = null;
		try {
			roleList =  roleService.getAllRole();
		} catch (Exception e) {
			log.info("【系统管理/用户管理/角色设置】查询所有角色异常！"+ e.toString());
		}
		Iterator<?> it = roleList.iterator();
		while(it.hasNext()) {
			Role role = (Role) it.next();
			if(user.getRole().getRoleCode().equals(role.getRoleCode())) {
				role.setSelected("checked");
			} else {
				role.setSelected("");
			}
		}
		
		request.setAttribute("userID", userID);
		request.setAttribute("roleList", roleList);
		request.setAttribute("position", "用户/角色设置");
		return mapping.findForward("changeRole");
	}
	
	// 角色设置，处理
	public ActionForward changeRole_Do(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		UserForm f = (UserForm) form;
		String roleID = f.getRoleID(); // 角色UD
		String userID = f.getUserID(); // 用户ID
		// 角色
		Role role = null;
		try {
			role = roleService.getRoleByRoleID(roleID);
		} catch (Exception e) {
			log.info("【系统管理/用户管理/角色设置】根据角色ID查询该角色异常！"+ e.toString());
		}
		// 用户
		User user = null;
		try {
			user = userService.getUserByUserID(userID);
		} catch (Exception e) {
			log.info("【系统管理/用户管理/角色设置】根据用户ID查询该用户异常！"+ e.toString());
		}
		user.setRole(role);
		try {
			userService.saveOrUpdateUser(user);
		} catch (Exception e) {
			log.info("【系统管理/用户管理/角色设置】新增或更新用户异常！"+ e.toString());
		}

		request.setAttribute("position", "系统管理/用户");
		this.listUser(mapping, form, request, response);
		return mapping.findForward("listUser");
	}
	
}